﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BingVis.SilverlightClient.Common;
using Microsoft.VirtualEarth.MapControl;
using Microsoft.VirtualEarth.MapControl.Core;
using Microsoft.VirtualEarth.MapControl.Design;

namespace BingVis.SilverlightClient
{
    public static class VirtualEarthBehaviors
    {
        public static ILocation GetFlyToLatLong(Map obj)
        {
            return (ILocation)obj.GetValue(FlyToLatLongProperty);
        }

        public static void SetFlyToLatLong(Map obj, ILocation propVal)
        {
            obj.SetValue(FlyToLatLongProperty, propVal);
        }

        public static readonly DependencyProperty FlyToLatLongProperty =
            DependencyProperty.RegisterAttached("FlyToLatLong", typeof(ILocation), typeof(VirtualEarthBehaviors), new PropertyMetadata(new LocationBase(0, 0), OnFlyToLatLongChanged));

        private static void OnFlyToLatLongChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {   
			// TODO: Some Type checking... get to it...
            var sender = (Map)depObj;
            var newVal = (ILocation)e.NewValue;

            // TODO: Some stuff with the new value.
            GoToLocation(sender, newVal);
        }

        public static ILocation GetStartupLatLong(Map obj)
        {
            return (ILocation)obj.GetValue(StartupLatLongProperty);
        }

        public static void SetStartupLatLong(Map obj, ILocation propVal)
        {
            obj.SetValue(StartupLatLongProperty, propVal);
        }

        public static readonly DependencyProperty StartupLatLongProperty =
            DependencyProperty.RegisterAttached("StartupLatLong", typeof(ILocation), typeof(VirtualEarthBehaviors), new PropertyMetadata(new Location(), OnStartupLatLongChanged));

        private static void OnStartupLatLongChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {   
			// TODO: Some Type checking... get to it...
            var sender = (Map)depObj;
            var newVal = (ILocation)e.NewValue;

            // TODO: Some stuff with the new value.
            GoToLocation(sender, newVal);
        }

        private static void GoToLocation(Map sender, ILocation newVal)
        {
            if (newVal == null || sender == null)
                return;

            sender.AnimationLevel = AnimationLevel.Full;

            sender.View = GetVirtualEarthMapView(newVal);
        }

        private static MapViewSpecificationConverter viewSpecCvt = new MapViewSpecificationConverter();
        private static MapViewSpecification GetVirtualEarthMapView(ILocation newVal)
        {
            var zoomLevel = 20;
            return (MapViewSpecification)viewSpecCvt.ConvertFromString(String.Format("{0},{1},0.0000 {2}.0000 0.0000", newVal.Latitude, newVal.Longitude, zoomLevel));
        }
    }
}
